// var i;
// var AL_GAME_HUA_Instance = require("./AL_GAME_HUA_Instance");
// var AL_GAME_HUA_Common = require("./AL_GAME_HUA_Common");
// var AL_GAME_HUA_Define = require("./AL_GAME_HUA_Define");
// var AL_GAME_HUA_PopupSelf = require("./AL_GAME_HUA_PopupSelf");
// var AL_GAME_HUA_GoodsConfig = require("./AL_GAME_HUA_GoodsConfig");
// var h = cc._decorator;
// var _ = h.ccclass;
// var P = h.property;
// var g =
//     (h.menu,
//     (function (e) {
//         function t() {
//             var t = (null !== e && e.apply(this, arguments)) || this;
//             t.node_item = null;
//             t.node_content = null;
//             t.node_button_close = null;
//             return t;
//         }
//         __extends(t, e);
//         t.prototype.zhixing_chushihua_chuli = function (e) {
//             return __awaiter(this, void 0, void 0, function () {
//                 var t;
//                 var n;
//                 var o;
//                 var i;
//                 var r;
//                 var a;
//                 var s;
//                 var l;
//                 var p;
//                 var f;
//                 var h;
//                 var _;
//                 var P;
//                 var g;
//                 var y;
//                 return __generator(this, function () {
//                     this.options = e;
//                     t = this.node_content.children[0];
//                     this.node_button_close.active = !1;
//                     n = [];
//                     o = 0;
//                     for (i = this.options.rewards; o < i.length; o++) {
//                         if ((y = i[o]).goodsId != AL_GAME_HUA_GoodsConfig.GoodsId.no_ads) {
//                             AL_GAME_HUA_GoodsConfig.GoodsConfig.getInstance().jieshou_shangpin(y.goodsId, y.num);
//                             n.push(y);
//                         }
//                     }
//                     r = this.node_item.width;
//                     a = Math.floor((n.length - 1) / 5) + 1;
//                     s = Math.min(n.length, 5);
//                     l = 0 - ((s - 1) * (r + 20)) / 2;
//                     p = 0 + ((a - 1) * (r + 20)) / 2;
//                     this.node_content.height = 300 + (a - 1) * (r + 20);
//                     f = 0;
//                     h = function (e) {
//                         var n = cc.instantiate(_.node_item);
//                         n.parent = t;
//                         n.x = l + (f % s) * (r + 20);
//                         n.y = p - Math.floor(f / s) * (r + 20);
//                         n.active = !0;
//                         var o = n.getChildByName("icon").getComponent(cc.Sprite);
//                         n.getChildByName("count").getComponent(cc.Label).string = "x" + e.num;
//                         AL_GAME_HUA_GoodsConfig.GoodsConfig.getInstance()
//                             .jiazai_tubiao_icon(e.goodsId)
//                             .then(function (e) {
//                                 if (cc.isValid(o)) {
//                                     o.spriteFrame = e;
//                                     o.addAutoReleaseAssets(e);
//                                     AL_GAME_HUA_Common.Common.uniform_scale(o, 200, 200);
//                                 }
//                             });
//                         n.opacity = 0;
//                         n.scale = 0;
//                         cc.tween(n)
//                             .delay(0.1 * f)
//                             .to(
//                                 0.2,
//                                 {
//                                     opacity: 255,
//                                     scale: 1
//                                 },
//                                 {
//                                     easing: "backOut"
//                                 }
//                             )
//                             .start();
//                         f++;
//                     };
//                     _ = this;
//                     P = 0;
//                     for (g = n; P < g.length; P++) {
//                         y = g[P];
//                         h(y);
//                     }
//                     this.node_button_close.active = !0;
//                     return [2];
//                 });
//             });
//         };
//         t.prototype.onEnable = function () {
//             AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.ON_GAME_CLUB_HIDE, {
//                 node: this.node,
//                 hide: !0
//             });
//             AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.ON_FEED_BACK_HIDE, {
//                 node: this.node,
//                 hide: !0
//             });
//         };
//         t.prototype.onDisable = function () {
//             AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.ON_GAME_CLUB_HIDE, {
//                 node: this.node,
//                 hide: !1
//             });
//             AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.ON_FEED_BACK_HIDE, {
//                 node: this.node,
//                 hide: !1
//             });
//         };
//         t.prototype.xianshi_dongzuo = function () {
//             return Promise.resolve();
//         };
//         t.prototype.yincang_dongzuo = function () {
//             return Promise.resolve();
//         };
//         t.prototype.start = function () {
//             return __awaiter(this, void 0, void 0, function () {
//                 return __generator(this, function () {
//                     AL_GAME_HUA_Common.Common.bofang_yinxiao("eject");
//                     AL_GAME_HUA_Instance.Instance.xianshi_dingbu_tiao({
//                         target: this.node,
//                         showAll: !0
//                     });
//                     return [2];
//                 });
//             });
//         };
//         t.prototype.handle_button_callback = function (e, t) {
//             switch (t) {
//                 case "close":
//                     AL_GAME_HUA_Common.Common.bofang_yinxiao("receive");
//                     this.close();
//             }
//         };
//         __decorate([P(cc.Node)], t.prototype, "node_item", void 0);
//         __decorate([P(cc.Node)], t.prototype, "node_content", void 0);
//         __decorate([P(cc.Node)], t.prototype, "node_button_close", void 0);
//         return __decorate([_], t);
//     })(AL_GAME_HUA_PopupSelf.default));
// exports.default = g;



const AL_GAME_HUA_Instance = require("./AL_GAME_HUA_Instance");
const AL_GAME_HUA_Common = require("./AL_GAME_HUA_Common");
const AL_GAME_HUA_Define = require("./AL_GAME_HUA_Define");
const AL_GAME_HUA_PopupSelf = require("./AL_GAME_HUA_PopupSelf");
const AL_GAME_HUA_GoodsConfig = require("./AL_GAME_HUA_GoodsConfig");

const { ccclass, property, menu } = cc._decorator;

/**
 * @class RewardPopup
 * @description 展示奖励弹窗，处理奖励显示及关闭逻辑
 * @extends AL_GAME_HUA_PopupSelf.default
 */
@ccclass
class ReceivePanel extends AL_GAME_HUA_PopupSelf.default {
    /** @type {cc.Node} 奖励物品节点 */
    @property(cc.Node)
    node_item = null;

    /** @type {cc.Node} 内容节点 */
    @property(cc.Node)
    node_content = null;

    /** @type {cc.Node} 关闭按钮节点 */
    @property(cc.Node)
    node_button_close = null;

    /**
     * @function zhixing_chushihua_chuli
     * @description 执行初始化处理
     * @param {Object} options - 配置信息
     */
    async zhixing_chushihua_chuli(options) {
        this.options = options;

        const firstChild = this.node_content.children[0];
        this.node_button_close.active = false;

        // 筛选奖励列表，排除广告奖励
        const rewards = this.options.rewards.filter(reward => reward.goodsId !== AL_GAME_HUA_GoodsConfig.GoodsId.no_ads);
        
        // 更新商品配置
        rewards.forEach(reward => {
            AL_GAME_HUA_GoodsConfig.GoodsConfig.getInstance().jieshou_shangpin(reward.goodsId, reward.num);
        });

        const itemWidth = this.node_item.width;
        const rows = Math.ceil(rewards.length / 5);
        const columns = Math.min(rewards.length, 5);

        const startX = -((columns - 1) * (itemWidth + 20)) / 2;
        const startY = ((rows - 1) * (itemWidth + 20)) / 2;
        this.node_content.height = 300 + (rows - 1) * (itemWidth + 20);

        let rewardIndex = 0;

        for (const reward of rewards) {
            const clonedNode = cc.instantiate(this.node_item);
            clonedNode.parent = firstChild;
            clonedNode.x = startX + (rewardIndex % columns) * (itemWidth + 20);
            clonedNode.y = startY - Math.floor(rewardIndex / columns) * (itemWidth + 20);
            clonedNode.active = true;

            const iconSprite = clonedNode.getChildByName("icon").getComponent(cc.Sprite);
            const countLabel = clonedNode.getChildByName("count").getComponent(cc.Label);
            countLabel.string = `x${reward.num}`;

            AL_GAME_HUA_GoodsConfig.GoodsConfig.getInstance()
                .jiazai_tubiao_icon(reward.goodsId)
                .then(spriteFrame => {
                    if (cc.isValid(iconSprite)) {
                        iconSprite.spriteFrame = spriteFrame;
                        iconSprite.addAutoReleaseAssets(spriteFrame);
                        AL_GAME_HUA_Common.Common.uniform_scale(iconSprite, 200, 200);
                    }
                });

            clonedNode.opacity = 0;
            clonedNode.scale = 0;

            cc.tween(clonedNode)
                .delay(0.1 * rewardIndex)
                .to(0.2, { opacity: 255, scale: 1 }, { easing: "backOut" })
                .start();

            rewardIndex++;
        }

        this.node_button_close.active = true;
    }

    /**
     * @function onEnable
     * @description 当节点启用时隐藏游戏俱乐部和反馈按钮
     */
    onEnable() {
        AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.ON_GAME_CLUB_HIDE, {
            node: this.node,
            hide: true
        });

        AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.ON_FEED_BACK_HIDE, {
            node: this.node,
            hide: true
        });
    }

    /**
     * @function onDisable
     * @description 当节点禁用时显示游戏俱乐部和反馈按钮
     */
    onDisable() {
        AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.ON_GAME_CLUB_HIDE, {
            node: this.node,
            hide: false
        });

        AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.ON_FEED_BACK_HIDE, {
            node: this.node,
            hide: false
        });
    }

    /**
     * @function xianshi_dongzuo
     * @description 显示动作
     * @returns {Promise<void>}
     */
    xianshi_dongzuo() {
        return Promise.resolve();
    }

    /**
     * @function yincang_dongzuo
     * @description 隐藏动作
     * @returns {Promise<void>}
     */
    yincang_dongzuo() {
        return Promise.resolve();
    }

    /**
     * @function start
     * @description 初始化时播放音效并显示顶部条
     */
    async start() {
        AL_GAME_HUA_Common.Common.bofang_yinxiao("eject");
        AL_GAME_HUA_Instance.Instance.xianshi_dingbu_tiao({
            target: this.node,
            showAll: true
        });
    }

    /**
     * @function handle_button_callback
     * @description 处理按钮点击事件
     * @param {cc.Event.EventCustom} event - 事件对象
     * @param {string} action - 动作类型
     */
    handle_button_callback(event, action) {
        if (action === "close") {
            AL_GAME_HUA_Common.Common.bofang_yinxiao("receive");
            this.close();
        }
    }
}

exports.default = ReceivePanel;

